﻿using Autofac;
using MgSoft.Import.Exchange.ExWebApi.Db.Daos;
using MgSoft.Import.Exchange.ExWebApi.Db.Dtos;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;

namespace MgSoft.Import.Exchange.ExWebApi.Db.Services
{
    public class DbTaskService : IDbTaskService
    {
        private ISyncDataDao syncDataDao;

        public DbTaskService(ILifetimeScope lifetimeScope)
        {
            this.syncDataDao = lifetimeScope.Resolve<ISyncDataDao>();
        }

        public void Do(string billType,string billTypeName,IDbExchangeDto exchangeDto)
        {
            verify(billType, billTypeName);

            exchangeDto.SetDto_BillType(billType);
            exchangeDto.SetDto_BillTypeName(billType);

            var keyFieldValue = exchangeDto.GetDto_KeyFieldValue();
            var orgNumberValue = exchangeDto.GetDto_OrgNumberValue();
            if (syncDataDao.IsExist(keyFieldValue, orgNumberValue, billType))
            {
                UpdateData(exchangeDto);
            }
            else
            {
                AddData(exchangeDto);
            }
        }

        private void verify(string billType, string billTypeName)
        {
            if (string.IsNullOrEmpty(billType))
            {
                throw new ArgumentException($"billType不能为空");
            }

            if(string.IsNullOrEmpty(billTypeName))
            {
                throw new ArgumentException($"billTypeName不能为空");
            }

        }


        /// <summary>
        /// 重载Action，把数据写入数据库
        /// </summary>
        protected virtual void UpdateData(IDbExchangeDto dto)
        {
            var keyFieldValue = dto.GetDto_KeyFieldValue();
            var orgNumberValue = dto.GetDto_OrgNumberValue();
            var billType = dto.GetDto_BillType();
            var updateFieldValue = dto.GetDto_UpdateFieldValue();

            SyncDataDto syncDataDto = syncDataDao.Get(keyFieldValue, orgNumberValue, billType);
            if (updateFieldValue == syncDataDto.Biz_UpdateField)
            {
                return;
            }
            else
            {
                updateDbExchangeDto(syncDataDto, dto);
            }
        }

        protected virtual void updateDbExchangeDto(SyncDataDto syncExchangeDto, IDbExchangeDto dto)
        {
            syncExchangeDto.State = StateEnum.Update;
            syncExchangeDto.Biz_UpdateField = dto.GetDto_UpdateFieldValue();
            syncExchangeDto.LastUpdateTime = DateTime.Now;
            syncExchangeDto.Biz_JsonData = SerializeObject(dto);
            syncDataDao.Update(syncExchangeDto);
        }

        protected virtual void AddData(IDbExchangeDto dto)
        {
            SyncDataDto syncDataDto = BuildSyncExchangeDto(dto);
            syncDataDao.AddSyncData(syncDataDto);
        }

        protected virtual SyncDataDto BuildSyncExchangeDto(IDbExchangeDto dto)
        {
            return new SyncDataDto()
            {
                CreateTime = DateTime.Now,
                FirstSyncSucceTime = null,
                LastSyncSucceTime = null,
                State = StateEnum.New,
                IsSyncError = false,
                ErrorMessage = "",
                SyncCount = 0,
                SyncTime = null,
                CostTime = 0,
                OrgNumber = dto.GetDto_OrgNumberValue(),
                Biz_KeyField = dto.GetDto_KeyFieldValue(),
                //Biz_KeyField1 = GetDto_KeyFieldValue(1, dto),
                //Biz_KeyField2 = GetDto_KeyFieldValue(2, dto),
                //Biz_KeyField3 = GetDto_KeyFieldValue(3, dto),
                //Biz_KeyField4 = GetDto_KeyFieldValue(4, dto),
                //Biz_KeyField5 = GetDto_KeyFieldValue(5, dto),
                //Biz_KeyField6 = GetDto_KeyFieldValue(6, dto),
                //Biz_KeyField7 = GetDto_KeyFieldValue(7, dto),
                //Biz_KeyField8 = GetDto_KeyFieldValue(8, dto),
                //Biz_KeyField9 = GetDto_KeyFieldValue(9, dto),
                Biz_JsonData = SerializeObject(dto),
                Biz_Date = dto.GetDto_DateValue(),
                Biz_UpdateField = dto.GetDto_UpdateFieldValue(),
                Biz_BillType = dto.GetDto_BillType(),
                Biz_BillTypeName = dto.GetDto_BillTypeName()
            };
        }

        protected string SerializeObject(IDbExchangeDto dto)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(dto, new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Ignore,
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            });
        }
    }
}
